import router from "./router";
import { getUserName } from "./composables/auth";
import { toast } from "~/composables/util"

// 全局前置守卫
router.beforeEach((to, from, next)=>{
    console.log("全局前置守卫");
    console.log("to=",to);
    console.log("from=",from);

    const username = getUserName()
    console.log("username=",username);

    // 没有登录，强制跳转回登录页
    if(!username && to.path != "/login"){
        console.log("没有登录，强制跳转回登录页");
        return next({ path:"/login"})
    }

    // 防止重复登录
    if(username && to.path == "/login"){
        toast("请勿重复登录", "error")
        return next({ path:from.path ? from.path : "/"})
    }

    // 需要放行，不然界面都是空白的
    next()
})